package kit.views;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;

import kit.core.Core;
import kit.core.ShellException;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class RoundQuestionPanel extends KitPanel {
	private JTextArea textArea;

	/**
	 * Create the panel.
	 */
	public RoundQuestionPanel() {
		setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		panel.setBackground(UIManager.getColor("Button.background"));
		panel.setForeground(Color.WHITE);
		add(panel, BorderLayout.SOUTH);
		panel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));

		JButton btnGeneraDomanda = new JButton("Genera domanda");
		btnGeneraDomanda.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				KitFrame frame = (KitFrame) SwingUtilities
						.getRoot(RoundQuestionPanel.this);
				Core core = Core.obtainSingleton();

				File firstHalfFile = RoundQuestionPanel.this.showSaveDialog(
						"Salva la prima metà",
						"Sono necessari entrambe i file", frame);
				if (firstHalfFile == null) {
					return;
				}
				File secondHalfFile = RoundQuestionPanel.this.showSaveDialog(
						"Salva la seconda metà",
						"Sono necessari entrambe i file", frame);
				if (secondHalfFile == null) {
					return;
				}

				String question = RoundQuestionPanel.this.getQuestionTextArea()
						.getText();
				try {
					File output = RoundQuestionPanel.this
							.tempFileBuilder(firstHalfFile.getName());
					output.createNewFile();
					core.encode(question, output);
					core.outputSplitter(firstHalfFile, secondHalfFile, output);
					output.delete();
					core.setCurrentQuestion(question);
					frame.getGeneraDomandaMenuItem().setEnabled(false);
					frame.getGeneraRispostaMenuItem().setEnabled(true);
					
					frame.getCenterPanel().removeAll();
					frame.setVisible(true);
					frame.pack();
				} catch (IOException | InterruptedException | ShellException e1) {
					RoundQuestionPanel.this.showErrorDialog(e1.getMessage(),
							frame);
				}
			}

		});
		panel.add(btnGeneraDomanda);

		JPanel panel_1 = new JPanel();
		panel_1.setBackground(UIManager.getColor("Button.background"));
		add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new FormLayout(
				new ColumnSpec[] { FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("max(120dlu;default):grow"),
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC, }, new RowSpec[] {
						FormFactory.RELATED_GAP_ROWSPEC,
						RowSpec.decode("max(35dlu;default):grow"),
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC, }));

		JLabel lblDomanda = new JLabel("Testo domanda:");
		panel_1.add(lblDomanda, "2, 2, default, top");

		textArea = new JTextArea();
		textArea.setBorder(new LineBorder(new Color(0, 0, 0)));
		textArea.setEditable(false);
		panel_1.add(textArea, "4, 2, fill, fill");

	}

	protected JTextArea getQuestionTextArea() {
		return textArea;
	}
}
